package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entify.Dish;
import com.itheima.reggie.entify.DishFlavor;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;

    //新增同时，保存口味的数据
    @Override
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        //调用保存方法，保存菜品的基本信息到菜品表dish中
        this.save(dishDto);

        //菜品id
        Long dishId = dishDto.getId();

        //菜品口味
        List<DishFlavor> flavors = dishDto.getFlavors();

        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());

        //保存菜品口味数据到菜品口味表dish_flavor中
        dishFlavorService.saveBatch(flavors);
    }

    //根据id查询菜品信息和对应口味的信息
    @Override
    public DishDto getByIdFlavor(Long id) {
        //从dish表中查询菜品基本信息
        Dish dish = this.getById(id);

        //创建DishDto对象
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish, dishDto);

        //查询当前菜品对应的口味信息，从口味表中获取
        LambdaQueryWrapper<DishFlavor> qw = new LambdaQueryWrapper<>();

        qw.eq(DishFlavor::getDishId, dish.getId());

        List<DishFlavor> list = dishFlavorService.list(qw);
        dishDto.setFlavors(list);

        return dishDto;
    }

    //修改菜品及菜品口味
    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
        //获取dish表中的数据
        this.updateById(dishDto);

        //对口味表中的数据进行删除
        LambdaQueryWrapper<DishFlavor> qw = new LambdaQueryWrapper<>();
        qw.eq(DishFlavor::getDishId, dishDto.getId());
        dishFlavorService.remove(qw);

        //插入当前提交进来的新数据
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());

        //调用方法保存数据
        dishFlavorService.saveBatch(flavors);

    }
     //菜品的删除和批量删以及菜品口味的删除和批量删除
    @Override
    @Transactional
    public boolean deleteWithFlavor(List<Long> ids)  {
        LambdaUpdateWrapper<Dish> dluw = new LambdaUpdateWrapper<>();
        dluw.in(Dish::getId, ids);
        dluw.eq(Dish::getStatus, 1);
        dluw.set(Dish::getIsDeleted, 1);
        int count = this.count(dluw);
        if (count > 0) {
            return false;
        }
        this.removeByIds(ids);
        LambdaUpdateWrapper<DishFlavor> fluw = new LambdaUpdateWrapper<>();
        fluw.in(DishFlavor::getDishId, ids);
        fluw.set(DishFlavor::getIsDeleted,1);
        dishFlavorService.remove(fluw);

        return true;
    }
}